const express=require('express');
//引入连接池模块
const userPool=require('../pool.js');
//创建路由器对象
const r = express.Router();


//第三方图形验证码模块:
//下载  npm i svg-captcha
const svgCaptcha = require('svg-captcha');


//所有图形验证,服务器node一个程序出生一张图
r.get('/v1/code',(req, res)=>{

	//第一个版本:图形类型的验证码
	const cap = svgCaptcha.create({
			// 翻转颜色
			inverse: false,
			// 字体大小
			fontSize: 36,
			// 噪声线条数
			noise: 3,
			// 宽度
			width: 80,
			// 高度
			height: 40,
			//是否是彩色
			color:true,
			ignoreChars:"0o1i",
			size:4,//验证码长度
		});
	
	//第二个版本:算术计算
	// const cap = svgCaptcha.createMathExpr({
	// 	fontSize: 45,
	// 	mathMin:1,
	// 	mathMax:9,
	// 	mathOperator:"+-",
	// 	width: 110,
	//     height: 45,
	// 	color:true,
	// });
	req.session.captcha = cap.text; // session 存储验证码数值
	//console.log(req.session)
	res.type('svg'); // 响应的类型
	res.send(cap.data)
  });




//1.登录模块
//http://127.0.0.1:8080/pro2/v1/login
r.post("/v1/login",(req,res)=>{
	//获取用户输入验证码
	//console.log(code,req.session.captcha)
	//与系统验证 和用户输入验证码比较
	let code = req.body.code;
	let reg = new RegExp(`^${req.session.captcha}$`,"i");
	// console.log(req.session.captcha);
  if(!reg.test(code)){
		res.send("-1");    //不一样返回-1验证码输入错误
		return;
	}

	if(req.session.auth){
		res.send("1");
		return;
	}
	var _uname = req.body.uname;
	var _upwd = req.body.upwd;
	let reg1 = new RegExp("^[a-z1-9]\\w{1,}@[a-z1-9]{2,5}\.[a-z]{3,5}$","i");
	if(!reg1.test(_uname)) {
		var sql = "select * from ht_user where uname=? and upwd=?";
	}else {
		var sql = "select * from ht_user where email=? and upwd=?";
	}
	userPool.query(sql,[_uname,_upwd],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			req.session.auth = true;  //登录成功auth
			res.send({code: 1,msg: "登录成功",data: result});//登录成功
		}else{
			req.session.auth = false;
			res.send("0");//登录失败
		}
	});
});
//根据uname查询用户信息
r.get("/v1/search",(req,res)=>{
	console.log(req.session.auth);
	if(!req.session.auth){
		res.send({code: -1,msg: "请先登录"});
		return;
	}
	var _name = req.query.uname;
	console.log(_name);
	var sql="select * from ht_user where uname=?";
	userPool.query(sql,[_name],(err,result)=>{
		if(err) throw err;
		if(!result.length) {
      res.send({code: 0,msg: "失败"});
    }else {
      res.send({code: 1,msg: "成功",data: result[0]});
    }
	});
});
//根据uid修改用户信息
r.put("/v1/update",(req,res)=>{
	var obj=req.body;
	var sql="update ht_user set email = ? where uid=?"; 
	console.log(sql,obj);
	userPool.query(sql,[obj.email,obj.uid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1");
		}else{
			res.send("0");
		}
	});
});

//注册用户
r.post("/v1/reg",(req,res)=>{
	// var obj=req.body;
	let code = req.body.code;
	let reg = new RegExp(`^${req.session.captcha}$`,"i");
	// console.log(req.session.captcha);
  if(!reg.test(code)){
		res.send("-1");    //不一样返回-1验证码输入错误
		return;
	}
	let obj = {
    uid: null,
    uname: req.body.uname,
    upwd: req.body.upwd,
    email: req.body.email,
    gender: null,
    birthday: null,
    resume: null
  };
	var sql="INSERT INTO ht_user set ?";
	console.log(obj);
	userPool.query(sql,[obj],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1");
		}else{
			res.send("0");
		}
	});
});

//判断用户名是否存在
// r.get("/v1/logout",(req,res)=>{
// 	req.session.destroy();
// 	res.send({code:1,msg:"退出成功"})
// });

//导出路由器对象
module.exports = r ;